﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 车辆日程
    /// </summary>
    public class CBizVehicleSchedule : CustBizObject
    {
        private CDmDjkVehicleSchedule m_CDmDjkVehicleSchedule = new CDmDjkVehicleSchedule();
        private CDmDjkVehicleSchedule oDmDjkVehicleSchedule
        {
            get { return m_CDmDjkVehicleSchedule; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizVehicleSchedule()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkVehicleSchedule.DJK_VEHICLE_SCHEDULE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["VEHICLE_SCHEDULE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }


        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        ///获取车辆日程 信息
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="vehicleId">车辆ID</param>
        /// <returns>DataSet</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-7 11:11
        public DataSet GetVehicleByID(string startTime, string endTime, string vehicleId)
        {

            if (string.IsNullOrEmpty(vehicleId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("vehicleId", ParameterDirection.Input, EDbType.String, 36, vehicleId));
            parmCollection.Add(new CDbParm("sTime", ParameterDirection.Input, EDbType.DateTime, 36, startTime));
            parmCollection.Add(new CDbParm("eTime", ParameterDirection.Input, EDbType.DateTime, 36, endTime));

            return this.DataObj.QueryBySQL(@"SELECT VEHICLE_SCHEDULE_ID,[VEHICLE_INFO_ID],[SCHEDULE_TITLE],[SCHEDULE_DESCRIPTION],[START_TIME],[END_TIME],[SCHEDULE_COLOR]
 FROM [dbo].[DJK_VEHICLE_SCHEDULE] WHERE SCHEDULE_STATUS=0 AND VEHICLE_INFO_ID=@vehicleId AND (START_TIME>=@sTime AND START_TIME<=@eTime)", parmCollection);

        }

        /// <summary>
        /// 添加车辆日程
        /// </summary>
        /// <param name="vehicleId">车辆ID</param>
        /// <param name="title">日程标题</param>
        /// <param name="description">日程备注</param>
        /// <param name="startTime">日程开始时间(如果只有一天，只用填写开始时间)</param>
        /// <param name="endTime">日程结束时间</param>
        /// <param name="color">red  代表用户颜色，其他设置 自选颜色</param>
        /// <param name="createUserID">创建人id</param>
        /// <param name="status">0 启用 1 禁用</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-7 11:14
        public string[] AddVehicleSchedule(string vehicleId, string title, string description, string startTime, string endTime, string color, string createUserID,int status)
        {

            if (string.IsNullOrEmpty(vehicleId.Trim()))
            {
                return new string[] { "false", "车辆不存在" };
            }
            if (string.IsNullOrEmpty(title.Trim()))
            {
                return new string[] { "false", "请填写日程标题" };
            }
            if (string.IsNullOrEmpty(createUserID.Trim()))
            {
                return new string[] { "false", "请添加创建人" };
            }
            if (string.IsNullOrEmpty(startTime.Trim()) && string.IsNullOrEmpty(endTime))
            {
                return new string[] { "false", "请填写车辆日程时间段" };
            }
            string isUpdate = UpdateVehicleSchedule(vehicleId, startTime, status);
            //
            if (!string.IsNullOrEmpty(isUpdate))
            {
                return new string[] { "true", isUpdate};
            }

            DataSet ds = new CDmDjkVehicleSchedule();
            DataRow dr = ds.Tables[0].NewRow();

            dr["VEHICLE_SCHEDULE_ID"] = this.DataObj.NewID();
            dr["VEHICLE_INFO_ID"] = vehicleId;
            dr["SCHEDULE_TITLE"] = title;
            dr["SCHEDULE_DESCRIPTION"] = description;
            dr["START_TIME"] = startTime;
            if (!string.IsNullOrEmpty(endTime))
            {
                dr["END_TIME"] = endTime;
            }
            dr["SCHEDULE_COLOR"] = color;
            dr["CREATE_TIME"] = this.dtCurrDateTime;

            dr["CREATE_USERID"] = createUserID;
            dr["SCHEDULE_STATUS"] = 0;
            ds.Tables[0].Rows.Add(dr);
            this.Update(ds);

            return new string[] { "true", dr["VEHICLE_SCHEDULE_ID"].ToString() };
        }
        /// <summary>
        /// 修改日程 添加用
        /// </summary>
        /// <param name="vehicleId">日程id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="status">启用状态</param>
        /// <returns></returns>
        ///  Author:蒲崇彬
        /// Creation date:2015-9-8 17:26
        public string UpdateVehicleSchedule(string vehicleId, string startTime,int status)
        {
            if (string.IsNullOrEmpty(vehicleId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("vehicleId", ParameterDirection.Input, EDbType.String, 36, vehicleId));
            parmCollection.Add(new CDbParm("sTime", ParameterDirection.Input, EDbType.DateTime, 36, startTime));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT VEHICLE_SCHEDULE_ID,[VEHICLE_INFO_ID],[SCHEDULE_TITLE],[SCHEDULE_DESCRIPTION],[START_TIME],[END_TIME],[SCHEDULE_COLOR],SCHEDULE_STATUS
 FROM [dbo].[DJK_VEHICLE_SCHEDULE] WHERE VEHICLE_INFO_ID=@vehicleId AND (START_TIME>=@sTime AND START_TIME<=@sTime)", parmCollection);

            if (ds == null || ds.Tables[0].Rows.Count != 1)
            {
                return null;
            }
            ds.Tables[0].Rows[0]["SCHEDULE_STATUS"] = status;
            ds.Tables[0].TableName = this.TableName;

            this.Update(ds);

            return ds.Tables[0].Rows[0]["VEHICLE_SCHEDULE_ID"].ToString(); ;
        }

        /// <summary>
        /// 修改日程
        /// </summary>
        /// <param name="scheduleid">日程id</param>
        /// <param name="status">是否 删除</param>
        /// <returns>string</returns>
        ///  Author:蒲崇彬
        /// Creation date:2015-9-8 15:14
        public string UpdateVehicleSchedule(string scheduleid,int status)
        {
            if (string.IsNullOrEmpty(scheduleid))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("scheduleid", ParameterDirection.Input, EDbType.String, 36, scheduleid));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT VEHICLE_SCHEDULE_ID,[VEHICLE_INFO_ID],[SCHEDULE_TITLE],[SCHEDULE_DESCRIPTION],[START_TIME],[END_TIME],[SCHEDULE_COLOR],SCHEDULE_STATUS
 FROM [dbo].[DJK_VEHICLE_SCHEDULE] WHERE VEHICLE_SCHEDULE_ID=@scheduleid ", parmCollection);

            if (ds == null || ds.Tables[0].Rows.Count != 1)
            {
                return null;
            }
            ds.Tables[0].Rows[0]["SCHEDULE_STATUS"] = status;
            ds.Tables[0].TableName = this.TableName;

            this.Update(ds);

            return ds.Tables[0].Rows[0]["VEHICLE_SCHEDULE_ID"].ToString();
        }
    }
}
